const { getDb } = require('../database');

class PublishNewsModel {
  /**
   * 获取所有发布的新闻
   * @returns {Promise<Array>} 发布的新闻列表
   */
  static async getAllPublishNews() {
    const db = await getDb();
    return db.all('SELECT * FROM publish_news');
  }

  /**
   * 根据ID获取发布的新闻
   * @param {number} id - 发布新闻ID
   * @returns {Promise<Object|null>} 发布新闻信息
   */
  static async getPublishNewsById(id) {
    const db = await getDb();
    return db.get('SELECT * FROM publish_news WHERE id = ?', [id]);
  }

  /**
   * 根据新闻ID获取发布的新闻
   * @param {number} newsId - 新闻ID
   * @returns {Promise<Array>} 发布的新闻列表
   */
  static async getPublishNewsByNewsId(newsId) {
    const db = await getDb();
    return db.all('SELECT * FROM publish_news WHERE news_id = ?', [newsId]);
  }

  /**
   * 根据状态获取发布的新闻
   * @param {number} pubState - 发布状态
   * @returns {Promise<Array>} 发布的新闻列表
   */
  static async getPublishNewsByState(pubState) {
    const db = await getDb();
    return db.all('SELECT * FROM publish_news WHERE pub_state = ?', [pubState]);
  }

  /**
   * 创建发布新闻
   * @param {Object} publishNewsData - 发布新闻数据
   * @returns {Promise<number>} 新创建的发布新闻ID
   */
  static async createPublishNews(publishNewsData) {
    const { news_id, news_title, class_real, class_model, abstract_real, news_source, news_time, create_time, publish_time, wait_pub_time, op_person, pub_state, cover_image } = publishNewsData;
    const db = await getDb();
    const result = await db.run(
      'INSERT INTO publish_news (news_id, news_title, class_real, class_model, abstract_real, news_source, news_time, create_time, publish_time, wait_pub_time, op_person, pub_state, cover_image) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
      [news_id, news_title, class_real, class_model, abstract_real, news_source, news_time, create_time, publish_time, wait_pub_time, op_person, pub_state || 0, cover_image]
    );
    return result.lastID;
  }

  /**
   * 更新发布新闻
   * @param {number} id - 发布新闻ID
   * @param {Object} publishNewsData - 发布新闻数据
   * @returns {Promise<boolean>} 是否更新成功
   */
  static async updatePublishNews(id, publishNewsData) {
    const { news_id, news_title, class_real, class_model, abstract_real, news_source, news_time, create_time, publish_time, wait_pub_time, op_person, pub_state, cover_image } = publishNewsData;
    const db = await getDb();
    const result = await db.run(
      'UPDATE publish_news SET news_id = ?, news_title = ?, class_real = ?, class_model = ?, abstract_real = ?, news_source = ?, news_time = ?, create_time = ?, publish_time = ?, wait_pub_time = ?, op_person = ?, pub_state = ?, cover_image = ? WHERE id = ?',
      [news_id, news_title, class_real, class_model, abstract_real, news_source, news_time, create_time, publish_time, wait_pub_time, op_person, pub_state, cover_image, id]
    );
    return result.changes > 0;
  }

  /**
   * 删除发布新闻
   * @param {number} id - 发布新闻ID
   * @returns {Promise<boolean>} 是否删除成功
   */
  static async deletePublishNews(id) {
    const db = await getDb();
    const result = await db.run('DELETE FROM publish_news WHERE id = ?', [id]);
    return result.changes > 0;
  }
}

module.exports = PublishNewsModel;